package edu.umassd.hciproject;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MyListsActivity extends Activity implements DialogInterface.OnClickListener, OnItemClickListener {

	ArrayAdapter<String> listAdapter;
	ListView listView;
	Button bAddList;
	MyListsManager manager;
	String selection = null;
	boolean editing = false;
	public static final String MY_LIST_NAME_KEY = "list_name";
	public static final String MY_LIST_PRODUCTS_KEY = "list_products";
	public static final String[] options = {"Open","Delete","Rename"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_lists);
		manager = new MyListsManager(this);
		
		this.setTitle(R.string.title_activity_my_lists);
		
		bAddList = (Button) findViewById(R.id.bAddList);
		bAddList.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editing = true;
				startActivity(new Intent(MyListsActivity.this, NewListActivity.class));
			}
		});
		listView = (ListView) findViewById(R.id.lvMyLists);
		listView.setOnItemClickListener(this);
		loadList();
	}

	private void loadList() {
		// TODO Auto-generated method stub
		listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, manager.getNames());
		if (manager.getNames().isEmpty())
			Toast.makeText(this, "You don't have any list", Toast.LENGTH_LONG).show();
		listView.setAdapter(listAdapter);
	}

	@Override
	public void onClick(DialogInterface dialog, int item) {
		// TODO Auto-generated method stub
		if (options[item].contentEquals("Open")) {
			Intent intent = new Intent(this, NewListActivity.class);
			intent.putExtra(MY_LIST_NAME_KEY, selection);
			startActivity(intent);
		} else if (options[item].contentEquals("Delete")) {
			manager.removeList(selection);
			listAdapter.remove(selection);
			listAdapter.notifyDataSetChanged();
		} else if (options[item].contentEquals("Rename")) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("Rename as...");
			EditText textBox = (EditText) getLayoutInflater().inflate(R.layout.simple_textbox, null);
			textBox.setText(selection);
			builder.setView(textBox);
			builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					EditText textBox = (EditText) ((AlertDialog) dialog).findViewById(R.id.tvSimpleBox);
					String newName = textBox.getText().toString();
					if (newName.length() == 0)
					{
						Toast.makeText(MyListsActivity.this, "Empty name", Toast.LENGTH_SHORT).show();
						return;
					}
					
					ArrayList<String> products = MyListsActivity.this.manager.getList(selection);
					MyListsActivity.this.manager.removeList(selection);
					MyListsActivity.this.manager.putList(newName, products);
					listAdapter.remove(selection);
					listAdapter.add(newName);
					listAdapter.notifyDataSetChanged();
				}
			});
			builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			builder.show();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (editing) {
			manager.update();
			listAdapter.clear();
			loadList();
			listAdapter.notifyDataSetChanged();
			editing = false;
		}
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my_lists, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		selection = listAdapter.getItem(position);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this).setItems(options, this);
		builder.show();
		
	}

}
